package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

//    @RequiredPermission({"权限列表", "permission:list"})
    @RequestMapping("/list")
    public String list(@ModelAttribute QueryObject qo, Model model) {
        model.addAttribute("pageInfo", permissionService.query(qo));
        return "permission/list";
    }

//    @RequiredPermission({"权限删除", "permission:delete"})
    @RequestMapping("/delete")
    public String delete(Long id) {
        permissionService.delete(id);
        return "redirect:/permission/list.do";
    }
//    @RequiredPermission({"权限加载", "permission:reload"})
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
        JsonResult result = new JsonResult();
        try {
            permissionService.reload();
        }catch (Exception e){
            e.printStackTrace();
            result.mark("亲,执行失败");
        }
        return result;
    }
}
